package com.accountant.travelaccountant;

import java.util.ArrayList;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class EditAccount2_ListViewAdapter extends BaseAdapter {

	Views views;
	EditAccount2 editAccount2;
	ArrayList<String> memberList;
	private int radioButtonselected = -1;
	private RadioButton radioButton;
	private TextView textView;
	private static String titletextOfSelectedTextview;

	public EditAccount2_ListViewAdapter(EditAccount2 editAccount2,
			ArrayList<String> memberList) {
		this.memberList = memberList;
		this.editAccount2 = editAccount2;
	}

	private class Views {
		TextView textView;
		RadioButton radioButton;
	}

	@Override
	public int getCount() {
		return memberList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return memberList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		/* Creating an inflater */
		LayoutInflater layoutInflater = editAccount2.getLayoutInflater();
		if (convertView == null) {
			/*
			 * Create an object so that it can the references of all the
			 * controls
			 */
			views = new Views();
			/*
			 * Assigning the view of all the textviews which you want into every
			 * listview
			 */
			convertView = layoutInflater.inflate(
					R.layout.edit_account2_listview_adapter_listrow, null);
			/* Assigning the ids to store references */
			views.textView = (TextView) convertView
					.findViewById(R.id.textViewforedit_account2);
			views.radioButton = (RadioButton) convertView
					.findViewById(R.id.radioButtonforedit_account2);
			/* Assigning the object to the view */
			convertView.setTag(views);
			convertView.setTag(R.id.textViewforedit_account2, views.textView);
			convertView.setTag(R.id.radioButtonforedit_account2,
					views.radioButton);
		} else {
			/*
			 * Get view is called every time setAdapter is calling itself to add
			 * a view
			 */
			views = (Views) convertView.getTag();
		}
		/*
		 * Arraylists of hashmaps come and then every time a new hashmap is
		 * assigned
		 */
		views.textView.setText(memberList.get(position));
		views.radioButton.setTag(position);
		views.radioButton.setChecked(position == radioButtonselected);
		
		convertView= HelperClassForActivities.addColourToListView(position,convertView,editAccount2);

		views.textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				RelativeLayout layout = (RelativeLayout) v.getParent();
				textView = (TextView) layout
						.findViewById(R.id.textViewforedit_account2);

				setTitletextOfSelectedTextview(textView.getText().toString());
				if (radioButton == null) {
					/*
					 * If first time you are clicking the button then store the
					 * button for future reference
					 */
					radioButton = (RadioButton) layout
							.findViewById(R.id.radioButtonforedit_account2);
					/* And check that radio button */
					radioButton.setChecked(true);
					radioButtonselected = (Integer) ((RadioButton) layout
							.findViewById(R.id.radioButtonforedit_account2))
							.getTag();
					notifyDataSetChanged();
					return;
				}
				/*
				 * If radio button has already been checked then just uncheck it
				 */
				if (radioButton == (RadioButton) layout
						.findViewById(R.id.radioButtonforedit_account2)) {
					((RadioButton) layout
							.findViewById(R.id.radioButtonforedit_account2))
							.setChecked(false);
					radioButtonselected = (Integer) ((RadioButton) layout
							.findViewById(R.id.radioButtonforedit_account2))
							.getTag();
					notifyDataSetChanged();
					return;
				}
				/*
				 * If checking for the second time first we uncheck the first
				 * radio button
				 */
				radioButton.setChecked(false);
				/* Then we check the new radio button */
				((RadioButton) layout
						.findViewById(R.id.radioButtonforedit_account2))
						.setChecked(true);
				/* Then we store this radio button for future reference */
				radioButton = (RadioButton) layout
						.findViewById(R.id.radioButtonforedit_account2);
				radioButtonselected = (Integer) ((RadioButton) layout
						.findViewById(R.id.radioButtonforedit_account2))
						.getTag();
				notifyDataSetChanged();
			}
		});

		views.radioButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				radioButtonselected = (Integer) v.getTag();
				notifyDataSetChanged();
				RelativeLayout layout = (RelativeLayout) v.getParent();
				textView = (TextView) layout
						.findViewById(R.id.textViewforedit_account2);
				setTitletextOfSelectedTextview(textView.getText().toString());
			}
		});

		return convertView;
	}

	public static String getTitletextOfSelectedTextview() {
		return titletextOfSelectedTextview;
	}

	public static void setTitletextOfSelectedTextview(
			String titletextOfSelectedTextview) {
		EditAccount2_ListViewAdapter.titletextOfSelectedTextview = titletextOfSelectedTextview;
	}

}
